package com.site.user.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.site.user.pojo.TbDictionary;
import com.site.user.pojo.TbOperation;
import com.site.user.service.ITbDictionaryService;

@Controller
@RequestMapping("/dict")
public class TbDictionaryController extends BaseController {
	@Resource
	private ITbDictionaryService tbDictionaryService;

	/**
	 * 查询数据列表
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_getdict", method = RequestMethod.GET)
	public void getdict(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbOperation entity) {
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		String id = UUID.randomUUID().toString().replace("-", "");
		entity.setIdOperation(id);
		String website=ServletRequestUtils.getStringParameter(request, "systemKey", "");
		String model=ServletRequestUtils.getStringParameter(request, "tbaleKey", "");
		String filed=ServletRequestUtils.getStringParameter(request, "fieldKey", "");
		List<TbDictionary> cglist=tbDictionaryService.getDict(website, model, filed);
		List<Map> jsonList = new ArrayList();
		for (int i = 0; i < cglist.size(); i++) {
			Map datamap = new HashMap();

			TbDictionary cagory = (TbDictionary) cglist.get(i);
			datamap.put("key", cagory.getKeyValue());
			datamap.put("value", cagory.getKeyName());

			jsonList.add(datamap);
		}
		JSONObject json = new JSONObject();

		try {
			json.put("options", jsonList);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		out.println(json.toString());
	}


}
